package com.example.score.service.impl;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

import com.example.score.entity.TQuestionImg;
import com.example.score.mapper.TQuestionImgMapper;
import com.example.score.service.ITQuestionImgService;
import com.example.score.utils.DateUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 【请填写功能名称】Service业务层处理
 * 
 * @author andy
 * @date 2025-06-16
 */
@Service
public class TQuestionImgServiceImpl implements ITQuestionImgService
{
    @Autowired
    private TQuestionImgMapper tQuestionImgMapper;

    /**
     * 查询【请填写功能名称】
     * 
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public TQuestionImg selectTQuestionImgById(Long id)
    {
        return tQuestionImgMapper.selectTQuestionImgById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     * 
     * @param tQuestionImg 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<TQuestionImg> selectTQuestionImgList(TQuestionImg tQuestionImg)
    {
        return tQuestionImgMapper.selectTQuestionImgList(tQuestionImg);
    }

    /**
     * 新增【请填写功能名称】
     * 
     * @param tQuestionImg 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertTQuestionImg(TQuestionImg tQuestionImg)
    {
        tQuestionImg.setCreateTime(DateUtils.getNowDate());
        return tQuestionImgMapper.insertTQuestionImg(tQuestionImg);
    }

    /**
     * 修改【请填写功能名称】
     * 
     * @param tQuestionImg 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateTQuestionImg(TQuestionImg tQuestionImg)
    {
        tQuestionImg.setUpdateTime(DateUtils.getNowDate());
        return tQuestionImgMapper.updateTQuestionImg(tQuestionImg);
    }

    /**
     * 批量删除【请填写功能名称】
     * 
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteTQuestionImgByIds(Long[] ids)
    {
        return tQuestionImgMapper.deleteTQuestionImgByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     * 
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteTQuestionImgById(Long id)
    {
        return tQuestionImgMapper.deleteTQuestionImgById(id);
    }

    @Override
    public void edit(List<TQuestionImg> tQuestionImgs) {
        if(!ObjectUtils.isEmpty(tQuestionImgs)){
            List<String> uuids = tQuestionImgs.stream().distinct().map(TQuestionImg::getQuestionUuid).collect(Collectors.toList());
            List<TQuestionImg> oldImgs = tQuestionImgMapper.selectByUuids(uuids);
            // 构建匹配索引：(question_uuid + "_" + type) → TQuestionImg
            Map<String, TQuestionImg> oldImgIndex = oldImgs.stream()
                    .collect(Collectors.toMap(
                            img -> img.getQuestionUuid() + "_" + img.getType(),
                            Function.identity(),
                            (existing, replacement) -> existing // 处理键冲突，保留第一个
                    ));
            for (TQuestionImg newImg : tQuestionImgs) {
                newImg.setCreateTime(DateUtils.getNowDate());
                // 构建匹配键
                String key = newImg.getQuestionUuid() + "_" + newImg.getType();
                // 判断是否存在匹配记录
                if (oldImgIndex.containsKey(key)) {
                    // 更新已有记录（设置主键ID）
                    TQuestionImg existingImg = oldImgIndex.get(key);
                    newImg.setId(existingImg.getId()); // 设置正确的主键ID
                    tQuestionImgMapper.updateTQuestionImg(newImg);
                } else {
                    // 插入新记录（主键ID由数据库自动生成或通过其他方式设置）
                    tQuestionImgMapper.insertTQuestionImg(newImg);
                }
            }
        }
    }

    @Override
    public List<TQuestionImg> selectListByUuids(List<String> uuids) {
        return tQuestionImgMapper.selectByUuids(uuids);
    }
}
